<template>
  <div class="pugtablebox">
    <div class="mb-3 flex justify-between">
      <div>
        <el-button size="small" type="success" @click="handleAdd" :icon="CirclePlus">新增</el-button>
        <el-button size="small" type="danger" :icon="Delete">批量删除</el-button>
      </div>
      <div>
        <el-input v-model="keyword" class="w-50 m-2" size="small" placeholder="请输入搜索关键词" :suffix-icon="Search" />
        <el-button size="small" type="success" :icon="Search">搜索</el-button>
      </div>
    </div>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column label="头像">
        <template #default="scope">
          <div style="display: flex; align-items: center">
            <el-avatar :size="50" src="http://tangzhe123-com.oss-cn-shenzhen.aliyuncs.com/public/60bf2fe6e9e53.jpg" />
            <div style="display: flex; flex-direction: column;">
              <span style="margin-left: 10px">yykk</span>
              <span class="mt-1" style="margin-left: 10px">ID：1258788</span>
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="Date">
        <template #default="scope">
          <div style="display: flex; align-items: center">
            <el-icon>
              <timer />
            </el-icon>
            <span style="margin-left: 10px">{{ scope.row.date }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="Name">
        <template #default="scope">
          <el-popover effect="light" trigger="hover" placement="top" width="auto">
            <template #default>
              <div>name: {{ scope.row.name }}</div>
              <div>address: {{ scope.row.address }}</div>
            </template>
            <template #reference>
              <el-tag>{{ scope.row.name }}</el-tag>
            </template>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="Operations">
        <template #default="scope">
          {{ scope.row.status }}
          <el-switch v-model="scope.row.status" class="ml-2" width="60px" inline-prompt :active-value="1"
            :inactive-value="0" active-text="发布" inactive-text="不发布" />
        </template>
      </el-table-column>
      <el-table-column label="Operations">
        <template #default="scope">
          <el-button size="small" :icon="Edit" @click="handleEdit(scope.$index, scope.row)">Edit</el-button>
          <el-button size="small" type="danger" :icon="Delete" @click="handleDelete(scope.$index, scope.row)">Delete
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination class="justify-center mt-5" background layout="prev, pager, next" :total="1000" />
  </div>

  <pug-drawer ref="formDrawerRef" title="修改管理员" @submit="handleSubmit">
     <el-form :model="manager" ref="managerFormRef" :rules="rules" label-width="80px" :inline="false" size="normal">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="manager.username" placeholder="用户名"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="manager.password" placeholder="密码" ></el-input>
      </el-form-item>
      <el-form-item label="头像" prop="avatar">
         <pug-choose-image v-model="manager.avatar"></pug-choose-image> 
      </el-form-item>
      <el-form-item label="所属角色" prop="role">
        <el-select v-model="manager.role" value-key="id" placeholder="请选择对应的角色" @change="">
          <el-option v-for="item in [{id:1,name:'超级管理员'}]"
          :key="item.id"
          :label="item.name"
          :value="item.id">
          </el-option>
        </el-select>
        
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-input v-model="manager.status" placeholder="所属状态"></el-input>
      </el-form-item>
    </el-form>
  </pug-drawer>



</template>

<script setup>
import { Timer } from '@element-plus/icons-vue'
import { ref, reactive } from 'vue'
import { toast,confirm } from '@/utils/utils.js'
import { CirclePlus, Top, Bottom, Delete, Edit, Search, Share, Upload } from '@element-plus/icons-vue'
import PugChooseImage from '../../components/PugChooseImage.vue';

const keyword = ref("");



const formDrawerRef = ref(null);
const managerFormRef = ref(null);

const handleAdd = () => {
  formDrawerRef.value.open();
}

const handleEdit = (index, row) => {
  console.log(index, row)
  formDrawerRef.value.open();
}
const handleDelete = (index, row) => {
  confirm("你确定要删除吗").then(res=>{
      toast("yes")
  })
  console.log(index, row)
}

const tableData = [
  {
    date: '2016-05-03',
    name: 'Tom',
    status: 1,
    address: 'No. 189, Grove St, Los Angeles',
  },
  {
    date: '2016-05-02',
    name: 'Tom',
    status: 0,
    address: 'No. 189, Grove St, Los Angeles',
  },
  {
    date: '2016-05-04',
    name: 'Tom',
    status: 1,
    address: 'No. 189, Grove St, Los Angeles',
  },
  {
    date: '2016-05-01',
    name: 'Tom',
    status: 0,
    address: 'No. 189, Grove St, Los Angeles',
  },
]


const rules =  {
  username: [{
    required: true,
    message: '用户名不能为空',
    trigger: 'blur'
  }],
  password: [{
    required: true,
    message: '密码不能为空',
    trigger: 'blur'
  }]
};

const manager = reactive({
  username: "",
  password: "",
  avatar: "",
  status: "",
  role: ""
})


const handleSubmit = ()=>{
   managerFormRef.value.validate((valid) => {
      if(!valid){
        return;
      }
      formDrawerRef.value.showLoading();
      alert("保存数据" + JSON.stringify(manager))
      formDrawerRef.value.close();
  })
}
</script>
